package com.a100.equipment.service.impl;

import com.a100.equipment.entity.EquCategory;
import com.a100.equipment.entity.vo.CreateCategoryVo;
import com.a100.equipment.entity.vo.PageCategoryVo;
import com.a100.equipment.entity.vo.UpdateCategoryVo;
import com.a100.equipment.mapper.EquCategoryMapper;
import com.a100.equipment.service.EquCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 设备分类 服务实现类
 * </p>
 *
 * @author index
 * @since 2021-12-02
 */
@Service
public class EquCategoryServiceImpl extends ServiceImpl<EquCategoryMapper, EquCategory> implements EquCategoryService {

    //新增设备类别
    @Override
    public boolean saveEquCategoryByVo(CreateCategoryVo createCategoryVo) {
        if(!StringUtils.isEmpty(createCategoryVo)){//传入参数vo不为空
            if(StringUtils.isEmpty(createCategoryVo.getCname())){
                //传入名称为空
                return false;
            }else {
                //传入名称不为空
                EquCategory equCategory = new EquCategory();
                //封装数据
                BeanUtils.copyProperties(createCategoryVo,equCategory);
                if(StringUtils.isEmpty(createCategoryVo.getSort())){
                    //传入排序为空，设置默认排序1024
                    equCategory.setSort(1024);
                }
                if(StringUtils.isEmpty(createCategoryVo.getState())){
                    //传入状态为空，设置默认状态1启用
                    equCategory.setState(1);
                }
                //调用数据库插入
                return this.save(equCategory);
            }
        }
        return false;
    }

    //修改设备类别
    @Override
    public boolean updateEquCategoryByVo(UpdateCategoryVo updateCategoryVo) {
        //传入参数vo不为空
        if(!StringUtils.isEmpty(updateCategoryVo)){
            if(StringUtils.isEmpty(updateCategoryVo.getId())){
                //id为空，直接返回false
                return false;
            }
            //根基id查询数据
            EquCategory equCategory = this.getById(updateCategoryVo.getId());
            if(StringUtils.isEmpty(equCategory)){
                //查无数据
                return false;
            }
            //填装修改的数据
            if(!StringUtils.isEmpty(updateCategoryVo.getCname())){
                //修改名称
                equCategory.setCname(updateCategoryVo.getCname());
            }
            if(!StringUtils.isEmpty(updateCategoryVo.getSort())){
                //修改排行
                equCategory.setSort(updateCategoryVo.getSort());
            }
            if(!StringUtils.isEmpty(updateCategoryVo.getState())){
                //修改状态
                equCategory.setState(updateCategoryVo.getState());
            }
            //重新插入数据库
            QueryWrapper<EquCategory> wrapper = new QueryWrapper<EquCategory>();
            wrapper.eq("id",equCategory.getId());
            return this.update(equCategory,wrapper);
        }
        return false;
    }
    //条件分页查询所有分类
    @Override
    public HashMap<String, Object> listPageByQuery(Integer current, Integer pagesize, PageCategoryVo pageCategoryVo) {
        Page<EquCategory> page = new Page<>(current,pagesize);
        //封装条件
        QueryWrapper<EquCategory> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        if(!StringUtils.isEmpty(pageCategoryVo)){
            if(!StringUtils.isEmpty(pageCategoryVo.getCname())){
                //模糊查询:名称
                wrapper.like("cname",pageCategoryVo.getCname());
            }
            if(!StringUtils.isEmpty(pageCategoryVo.getSort())){
                //排序
                wrapper.orderByAsc("sort");
            }
            if(!StringUtils.isEmpty(pageCategoryVo.getState())){
                //精确查询:状态
                wrapper.eq("state",pageCategoryVo.getState());
            }
        }
        IPage<EquCategory> iPage = this.page(page, wrapper);
        //组装data
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",page.getTotal());
        hashMap.put("rows",page.getRecords());
        return hashMap;
    }
    //根据id查询类型
    @Override
    public EquCategory chooseEquCategoryById(String equCategoryId) {
        //查询出数据
        EquCategory category = this.getById(equCategoryId);
        //判断数据有效性
        if(!StringUtils.isEmpty(category)){
            return category;
        }
        return null;
    }
    //查询所有类型
    @Override
    public List<EquCategory> chooseAllEquCategory() {
        //封装条件
        QueryWrapper<EquCategory> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        List<EquCategory> equCategoryList = this.list(wrapper);
        //判断数据有效性
        if(!StringUtils.isEmpty(equCategoryList)){
            return equCategoryList;
        }
        return null;
    }

}
